//
//  UIView+WTLoading.m
//  66TV
//
//  Created by ben on 16/8/14.
//  Copyright © 2016年 shifeng. All rights reserved.
//

#import "UIView+WTLoading.h"
#import "UIImageView+WTLoading.h"
#import "UIColor+WTExtend.h"
#import "UIImage+WTExtend.h"
#import "Masonry.h"
#import <objc/message.h>

static const void *GifKey = &GifKey;
@implementation UIView (WTLoading)
- (UIImageView *)gifView
{
    return objc_getAssociatedObject(self, GifKey);
}

- (void)setGifView:(UIImageView *)gifView
{
    objc_setAssociatedObject(self, GifKey, gifView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)setLoadingLabel:(UILabel *)loadingLabel{
    objc_setAssociatedObject(self, @selector(loadingLabel), loadingLabel, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UILabel *)loadingLabel{
    return objc_getAssociatedObject(self, _cmd);
}

// 显示GIF加载动画
- (void)showGifLoding
{
   
   [self hideGifLoding];
   NSArray* images = @[[UIImage wt_imageWithName:@"wt_refreshImage_1"], [UIImage wt_imageWithName:@"wt_refreshImage_2"], [UIImage wt_imageWithName:@"wt_refreshImage_3"],[UIImage wt_imageWithName:@"wt_refreshImage_4"]];
    
    UIImageView *gifView = [[UIImageView alloc] init];
    [self addSubview:gifView];
    [gifView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(@0);
        make.width.equalTo(@60);
        make.height.equalTo(@60);
        make.centerY.mas_equalTo(self.mas_centerY).offset(-20);
    }];
    self.gifView = gifView;
    
    UILabel *loadingLabel = [[UILabel alloc] init];
    loadingLabel.font = [UIFont systemFontOfSize:14];
    loadingLabel.textColor = [UIColor hexChangeFloat:@"969696"];
    loadingLabel.text = @"加载中...";
    [self addSubview:loadingLabel];
    [loadingLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.gifView.mas_centerX);
        make.top.mas_equalTo(self.gifView.mas_bottom);
        make.height.mas_equalTo(20);
    }];
    self.loadingLabel = loadingLabel;
    [self addSubview:loadingLabel];
    
    [gifView playGifAnim:images];
    
}

- (void)setLoadingText:(NSString*)loadingText{
    self.loadingLabel.text = loadingText;
}

// 取消GIF加载动画
- (void)hideGifLoding
{
    [self.gifView stopGifAnim];
    self.gifView = nil;
    
    [self.loadingLabel removeFromSuperview];
    self.loadingLabel = nil;
}
@end
